<template>
  <div>
    <el-row class="warp">
      <el-col :span="24" class="warp-breadcrum">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }"><b>系统管理</b></el-breadcrumb-item>
          <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>

      <el-col :span="24" class="warp-main" v-loading="loading" element-loading-text="拼命加载中">
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 10px;">
          <!--<el-form :inline="true" :model="filters">-->
            <!--<el-form-item>-->
              <!--<el-input v-model="filters.name" placeholder="角色名/姓名/ 昵称" style="min-width: 240px;"-->
                        <!--@keyup.enter.native="handleSearch"></el-input>-->
            <!--</el-form-item>-->
            <!--<el-form-item>-->
              <!--&lt;!&ndash;<el-button type="primary" @click="handleSearch">查询</el-button>&ndash;&gt;-->
            <!--</el-form-item>-->
            <!--<el-form-item style="float: right">-->
              <!--&lt;!&ndash;<el-button type="primary" @click="openDomainDialog()">新增</el-button>&ndash;&gt;-->
            <!--</el-form-item>-->
          <!--</el-form>-->
        </el-col>

        <!--列表-->
        <el-table :data="domainList" highlight-current-row v-loading="loading" style="width: 100%;">
          <el-table-column type="index" width="60">
          </el-table-column>
          <el-table-column prop="roleName" label="角色名称" sortable>
          </el-table-column>
          <el-table-column prop="remark" label="备注" width="100">
          </el-table-column>
          <!--<el-table-column property="operate" label="操作" width="150">-->
            <!--<template scope="scope">-->
              <!--&lt;!&ndash;<el-button&ndash;&gt;-->
                <!--&lt;!&ndash;size="mini" type="primary" @click="openEditDomainDialog(scope.row)">编辑&ndash;&gt;-->
              <!--&lt;!&ndash;</el-button>&ndash;&gt;-->
              <!--&lt;!&ndash;<el-button&ndash;&gt;-->
                <!--&lt;!&ndash;size="mini"&ndash;&gt;-->
                <!--&lt;!&ndash;type="danger" @click="delDomain(scope.row.id)">删除&ndash;&gt;-->
              <!--&lt;!&ndash;</el-button>&ndash;&gt;-->
            <!--</template>-->
          <!--</el-table-column>-->
        </el-table>
      </el-col>
    </el-row>
    <el-dialog :title="domainDialog.title" :visible.sync="domainDialog.isShow" width="450px" :close-on-click-modal="false" @close="closeDomainDialogFn"
               :modal="true">
      <el-form :model="domainForm" :rules="rules" ref="domainForm">
        <el-form-item prop="roleName" label="角色名称" label-width="80px">
          <el-input size="medium" placeholder="请输入角色名称" v-model="domainForm.roleName" style="width: 217px"></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="备注" label-width="80px" style="margin-top: -10px">
          <el-input size="medium" placeholder="请输入备注" v-model="domainForm.remark" style="width: 217px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" type="default" @click="closeDialogFn(domainDialog.isShow = false)">取消</el-button>
        <el-button size="medium" type="primary" @click="submitDomainForm('domainForm')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import domainAPI from '../../api/api_role';

  export default {
    data() {
      return {
        filters: {
          name: ''
        },
        domainDialog: {
          isShow: false,
          title: '角色新增'
        },
        domainForm: {
          roleName: '',
          remark: ''
        },
        resetDomainForm: {
          roleName: '',
          remark: ''
        },
        rules: {
          email: [{required: true, type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}]
        },
        loading: false,
        domainList: [],
        total: 0,
        page: 1,
        limit: 10,
        loading: false
      }
    },
    methods: {
      handleCurrentChange(val) {
        this.page = val;
        this.search();
      },
      closeDialogFn() {
        this.domainDialog.isShow = false
      },
      submitDomainForm(formName) {
        let _this = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            domainAPI.doAddOrUpdate(this.domainForm).then(result => {
              _this.$message({
                message: result.message,
                type: 'success'
              })
              _this.handleSearch()
              _this.domainDialog.isShow = false
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      openEditDomainDialog (row) {
        this.domainDialog.isShow = true
        this.domainDialog.title = '编辑角色'
        Object.assign(this.domainForm, row)
      },
      delDomain (id) {
        let _this = this
        this.$confirm('此操作将删除角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          domainAPI.doDel({'id': id}).then(result => {
            _this.$message({
              message: result.message,
              type: 'success'
            })
            _this.handleSearch()
          })
        }).catch(() => {
        })
      },
      openDomainDialog () {
        this.domainDialog.isShow = true
        this.domainDialog.title = '新增角色'
        this.domainForm.id = -1
      },
      closeDomainDialogFn () {
        this.$refs['domainForm'].resetFields()
        Object.assign(this.domainForm, this.resetDomainForm)
      },
      handleSearch() {
        this.total = 0;
        this.page = 1;
        this.search();
      },
      //获取角色列表
      search: function () {
        let that = this;
        let params = {
          page: that.page,
          limit: 10,
          name: that.filters.name
        };

        that.loading = true;
        domainAPI.qryByPage(params).then(function (result) {
          that.loading = false;
          if (result) {
            that.total = 10;
            that.domainList = result.data;
          }
        }, function (err) {
          that.loading = false;
          that.$message.error({showClose: true, message: err.toString(), duration: 2000});
        }).catch(function (error) {
          that.loading = false;
          that.$message.error({showClose: true, message: '请求出现异常', duration: 2000});
        });
      }
    },
    mounted() {
      this.handleSearch()
    }
  }
</script>

<style scoped>

</style>
